package com.example.administrator.zkdemo.activity;

/**
 * Created with Android Studio.
 * 描述: ${DESCRIPTION}
 * Date: 2018/11/21
 * Time: 16:22
 *
 * @author 晏琦云
 * @version ${VERSION}
 */

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.zkdemo.adapter.ZKEmployeeadapter;
import com.example.administrator.zkdemo.db.orm.tna.UserInfo;
import com.example.administrator.zkdemo.model.ZKEmployeeBean;
import com.example.administrator.zkdemo.utils.ZKRecycleView;
import com.sensetime.mid.faceapi.FaceSearch;
import com.zkteco.android.db.orm.tna.PersBiotemplate;
import com.zkteco.android.db.orm.tna.PersBiotemplatedata;
import com.zkteco.android.utils.ZKFilePath;
import com.zkteco.android.utils.constant.ZKMapData;
import com.zkteco.android.utils.view.ZKAlertDialog;
import com.zkteco.android.utils.view.ZKAlertDialog.OnCancelButtonListener;
import com.zkteco.android.utils.view.ZKAlertDialog.OnSureButtonListener;
import com.zkteco.android.utils.view.ZKToast;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ZKEmployeeListActivity extends AppCompatActivity
        implements View.OnClickListener {
    private static final int maxSize = 30;
    private ImageView Cdel;
    private ImageView HeadAdd;
    private ImageView HeadDel;
    private RelativeLayout back;
    private boolean isDelete;
    private boolean isLoadMore;
    private ZKEmployeeadapter mAdapter;
    private List<ZKEmployeeBean> mData = new ArrayList();
    private EditText mEditSearch;
    private UpdateReceiver receiver;
    private String strQuerying = "";
    private TextView title;
    private List<UserInfo> userInfoList = new ArrayList();
    private ZKRecycleView userList;

    private void initData() {
        try {
            Object localObject = queryUserInfo(new UserInfo(), Long.valueOf(this.userInfoList.size()), 30L);
            if (localObject != null) {
                localObject = addData((List) localObject, this.userInfoList).iterator();
                while (((Iterator) localObject).hasNext()) {
                    UserInfo localUserInfo = (UserInfo) ((Iterator) localObject).next();
                    this.mData.add(new ZKEmployeeBean(localUserInfo));
                }
            }
            setAdapter(this.mData);
        } catch (SQLException localSQLException) {
            localSQLException.printStackTrace();
        }
        if (this.userInfoList.size() > 0) {
            this.isLoadMore = true;
            return;
        }
        this.isLoadMore = false;
    }

    private void initListener() {
        this.userList.setRefreshLoadListener(new ZKRecycleView.RefreshLoadListener() {
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                                              public void run() {
                                                  ZKEmployeeListActivity.this.loadMore();
                                              }
                                          }
                        , 300L);
            }

            public void onRefresh() {
            }
        });
        this.mAdapter.setClick(new ZKEmployeeadapter.EditClick() {
            public void btnState(boolean paramBoolean) {
                if (paramBoolean) {
                    ZKEmployeeListActivity.this.setDeleteState(0);
                    return;
                }
                ZKEmployeeListActivity.this.setDeleteState(1);
            }

            public void onClick(UserInfo paramUserInfo) {
                Intent localIntent = new Intent(ZKEmployeeListActivity.this, ZKEditEmployeeActivity.class);
                localIntent.putExtra("user_id", paramUserInfo.getID());
                ZKEmployeeListActivity.this.startActivity(localIntent);
            }
        });
    }

    private void initView() {
        Object localObject = new LinearLayoutManager(this);
        this.userList = ((ZKRecycleView) findViewById(2131296646));
        this.userList.setLayoutManager((RecyclerView.LayoutManager) localObject);
        this.userList.setPullRefreshEnable(false);
        this.title = ((TextView) findViewById(2131296585));
        this.HeadDel = ((ImageView) findViewById(2131296366));
        this.HeadAdd = ((ImageView) findViewById(2131296285));
        this.back = ((RelativeLayout) findViewById(2131296491));
        this.Cdel = ((ImageView) findViewById(2131296427));
        this.HeadDel.setOnClickListener(this);
        this.HeadAdd.setOnClickListener(this);
        this.Cdel.setOnClickListener(this);
        this.back.setOnClickListener(this);
        this.title.setText(getResources().getString(2131624086));
        this.mEditSearch = ((EditText) findViewById(2131296379));
        this.mEditSearch.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable paramEditable) {
            }

            public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
            }

            public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
                if (!paramCharSequence.toString().equals("")) {
                    ZKEmployeeListActivity.access$002(ZKEmployeeListActivity.this, paramCharSequence.toString());
                    ZKEmployeeListActivity.access$102(ZKEmployeeListActivity.this, ZKEmployeeListActivity.queryNameAndPin(new UserInfo(), ZKEmployeeListActivity.this.strQuerying, 0L, 30L));
                } else {
                    ZKEmployeeListActivity.access$002(ZKEmployeeListActivity.this, null);
                    try {
                        ZKEmployeeListActivity.access$102(ZKEmployeeListActivity.this, ZKEmployeeListActivity.queryUserInfo(new UserInfo(), Long.valueOf(0L), 30L));
                    } catch (SQLException paramCharSequence) {
                        paramCharSequence.printStackTrace();
                    }
                }
                if (ZKEmployeeListActivity.this.mData != null)
                    ZKEmployeeListActivity.this.mData.clear();
                if (ZKEmployeeListActivity.this.userInfoList != null) {
                    paramCharSequence = ZKEmployeeListActivity.this.userInfoList.iterator();
                    while (paramCharSequence.hasNext()) {
                        UserInfo localUserInfo = (UserInfo) paramCharSequence.next();
                        ZKEmployeeListActivity.this.mData.add(new ZKEmployeeBean(localUserInfo));
                    }
                    ZKEmployeeListActivity.this.mAdapter.setData(ZKEmployeeListActivity.this.mData);
                }
                ZKEmployeeListActivity.this.mAdapter.notifyDataSetChanged();
            }
        });
        this.receiver = new UpdateReceiver();
        localObject = new IntentFilter();
        ((IntentFilter) localObject).addAction("com.zkteco.android.ACTION_UPDATE");
        registerReceiver(this.receiver, (IntentFilter) localObject);
    }

    private void loadMore() {
        try {
            Object localObject;
            if (TextUtils.isEmpty(this.strQuerying))
                localObject = (ArrayList) queryUserInfo(new UserInfo(), Long.valueOf(this.userInfoList.size()), 30L);
            else
                localObject = (ArrayList) queryNameAndPin(new UserInfo(), this.strQuerying, this.userInfoList.size(), 30L);
            addData((List) localObject, this.userInfoList);
            if (localObject != null) {
                localObject = ((ArrayList) localObject).iterator();
                while (((Iterator) localObject).hasNext()) {
                    UserInfo localUserInfo = (UserInfo) ((Iterator) localObject).next();
                    this.mData.add(new ZKEmployeeBean(localUserInfo));
                }
                this.mAdapter.setData(this.mData);
            }
        } catch (SQLException localSQLException) {
            localSQLException.printStackTrace();
        }
        if (this.isLoadMore)
            this.userList.setPushRefreshEnable(true);
        else
            this.userList.setPushRefreshEnable(false);
        this.userList.setPullLoadMoreCompleted();
    }

    private void prepareShowDialog() {
        List localList = this.mAdapter.getData();
        if ((localList != null) && (!localList.isEmpty())) {
            int k = 0;
            int i = 0;
            int j;
            while (true) {
                j = k;
                if (i >= localList.size())
                    break;
                if (((ZKEmployeeBean) localList.get(i)).isBol()) {
                    j = 1;
                    break;
                }
                i += 1;
            }
            if (j != 0) {
                deleteShowDialog();
                return;
            }
            ZKToast.showToast(this, 2131624084);
            return;
        }
        ZKToast.showToast(this, 2131624076);
    }

    public static List<UserInfo> queryNameAndPin(UserInfo paramUserInfo, String paramString, long paramLong1, long paramLong2) {
        try {
            paramUserInfo = paramUserInfo.getQueryBuilder().offset(Long.valueOf(paramLong1)).limit(Long.valueOf(paramLong2)).where();
            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append("%");
            localStringBuilder.append(paramString);
            localStringBuilder.append("%");
            paramUserInfo = paramUserInfo.like("Name", localStringBuilder.toString()).or();
            localStringBuilder = new StringBuilder();
            localStringBuilder.append("%");
            localStringBuilder.append(paramString);
            localStringBuilder.append("%");
            paramUserInfo = paramUserInfo.like("User_PIN", localStringBuilder.toString()).query();
            return paramUserInfo;
        } catch (SQLException paramUserInfo) {
            paramUserInfo.printStackTrace();
        }
        return null;
    }

    public static List<UserInfo> queryUserInfo(UserInfo paramUserInfo, Long paramLong, long paramLong1)
            throws SQLException {
        return paramUserInfo.getQueryBuilder().offset(paramLong).limit(Long.valueOf(paramLong1)).query();
    }

    private void setAdapter(List<ZKEmployeeBean> paramList) {
        this.mAdapter = new ZKEmployeeadapter(this, paramList);
        this.userList.setAdapter(this.mAdapter);
    }

    private void setDeleteState(int paramInt) {
        switch (paramInt) {
            default:
                return;
            case 1:
                this.Cdel.setEnabled(false);
                this.Cdel.setImageResource(2131492871);
                return;
            case 0:
        }
        this.Cdel.setEnabled(true);
        this.Cdel.setImageResource(2131492870);
    }

    private void updateDate() {
        this.mData.clear();
        this.userInfoList = queryNameAndPin(new UserInfo(), this.strQuerying, 0L, 30L);
        if (this.userInfoList != null) {
            Iterator localIterator = this.userInfoList.iterator();
            while (localIterator.hasNext()) {
                UserInfo localUserInfo = (UserInfo) localIterator.next();
                this.mData.add(new ZKEmployeeBean(localUserInfo));
            }
        }
        this.mAdapter.notifyDataSetChanged();
    }

    public List<UserInfo> addData(List<UserInfo> paramList1, List<UserInfo> paramList2) {
        int i = 0;
        while (i < paramList1.size()) {
            paramList2.add((UserInfo) paramList1.get(i));
            i += 1;
        }
        return paramList2;
    }

    public void delData() {
        int i = this.mAdapter.getData().size() - 1;
        while (i >= 0) {
            ZKEmployeeBean localZKEmployeeBean = this.mAdapter.getItem(i);
            if (localZKEmployeeBean.isBol())
                try {
                    FaceSearch.delete((int) localZKEmployeeBean.getUserInfo().getID());
                    Object localObject1 = new StringBuilder();
                    ((StringBuilder) localObject1).append(ZKFilePath.PHOTO_PATH);
                    ((StringBuilder) localObject1).append(localZKEmployeeBean.getUserInfo().getUser_PIN());
                    ((StringBuilder) localObject1).append(".jpg");
                    localObject1 = new File(((StringBuilder) localObject1).toString());
                    Object localObject2 = new StringBuilder();
                    ((StringBuilder) localObject2).append(ZKFilePath.FACE_PATH);
                    ((StringBuilder) localObject2).append(localZKEmployeeBean.getUserInfo().getUser_PIN());
                    ((StringBuilder) localObject2).append(".jpg");
                    localObject2 = new File(((StringBuilder) localObject2).toString());
                    if (((File) localObject2).exists())
                        ((File) localObject2).delete();
                    if (((File) localObject1).exists())
                        ((File) localObject1).delete();
                    localObject1 = localZKEmployeeBean.getUserInfo().getUser_PIN();
                    localObject1 = (PersBiotemplate) new PersBiotemplate().getQueryBuilder().where().eq("user_pin", localObject1).queryForFirst();
                    if (localObject1 != null) {
                        int j = ((PersBiotemplate) localObject1).getTemplate_id();
                        ((PersBiotemplatedata) new PersBiotemplatedata().getQueryBuilder().where().eq("ID", Integer.valueOf(j)).queryForFirst()).delete();
                        ((PersBiotemplate) localObject1).delete();
                    }
                    localZKEmployeeBean.getUserInfo().delete();
                    ZKMapData.MEET_MAP.remove(localZKEmployeeBean.getUserInfo().getUser_PIN());
                    ZKMapData.ATT_MAP.remove(localZKEmployeeBean.getUserInfo().getUser_PIN());
                    this.mAdapter.remove(i);
                } catch (SQLException localSQLException) {
                    localSQLException.printStackTrace();
                }
            i -= 1;
        }
        this.mData = this.mAdapter.getData();
    }

    public void deleteShowDialog() {
        ZKAlertDialog localZKAlertDialog = new ZKAlertDialog(this, getResources().getString(2131624064));
        localZKAlertDialog.setonSureButtonListener(new ZKAlertDialog.OnSureButtonListener(localZKAlertDialog) {
            public void onSureButtonClick() {
                ZKEmployeeListActivity.this.delData();
                ZKEmployeeListActivity.this.mAdapter.setIsdelete(false);
                ZKEmployeeListActivity.this.setDeleteState(1);
                this.val$dialog.dismiss();
                ZKEmployeeListActivity.this.Cdel.setVisibility(8);
                ZKEmployeeListActivity.this.HeadAdd.setVisibility(0);
                ZKEmployeeListActivity.this.HeadDel.setVisibility(0);
            }
        });
        localZKAlertDialog.setCancelButtonListener(new ZKAlertDialog.OnCancelButtonListener(localZKAlertDialog) {
            public void onCancelButtonClick() {
                ZKEmployeeListActivity.this.setDeleteState(1);
                this.val$dialog.dismiss();
                ZKEmployeeListActivity.this.Cdel.setVisibility(8);
                ZKEmployeeListActivity.this.HeadAdd.setVisibility(0);
                ZKEmployeeListActivity.this.HeadDel.setVisibility(0);
            }
        });
        localZKAlertDialog.show();
    }

    public void onClick(View paramView) {
        int i = paramView.getId();
        if (i != 2131296285) {
            if (i != 2131296366) {
                if (i != 2131296427) {
                    if (i != 2131296491)
                        return;
                    finish();
                    return;
                }
                prepareShowDialog();
                return;
            }
            this.Cdel.setVisibility(0);
            this.HeadAdd.setVisibility(8);
            this.HeadDel.setVisibility(8);
            this.mAdapter.setIsdelete(true);
            this.isDelete = true;
            return;
        }
        startActivity(new Intent(this, ZKAddEmployeeActivity.class));
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(2131427360);
        initView();
        initData();
        initListener();
    }

    protected void onDestroy() {
        unregisterReceiver(this.receiver);
        super.onDestroy();
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if (paramInt == 4) {
            if (this.isDelete) {
                setDeleteState(1);
                this.mAdapter.setIsdelete(false);
                this.Cdel.setVisibility(8);
                this.HeadDel.setVisibility(0);
                this.HeadAdd.setVisibility(0);
                this.isDelete = false;
                return true;
            }
            finish();
            return true;
        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }

    protected void onRestart() {
        super.onRestart();
    }

    protected void onResume() {
        super.onResume();
    }

    class UpdateReceiver extends BroadcastReceiver {
        UpdateReceiver() {
        }

        public void onReceive(Context paramContext, Intent paramIntent) {
            ZKEmployeeListActivity.this.updateDate();
        }
    }
}
